﻿
namespace OpenShape
{
    class Scaler
    {
        public double mox, moy;
        public double mxMin, myMin, mxMax, myMax;
        public double sox, soy;
        public double sxMin, syMin, sxMax, syMax;

        public double nowRatio;

        public void DeviceToWorld(double sx, double sy, out double mx, out double my)
        {
            double m_lfnowXRatio = (mxMax - mxMin) / (sxMax - sxMin);  //x축척
            double m_lfnowYRatio = (myMax - myMin) / (syMax - syMin);  //y축척

            // 종횡비 보정
            if (m_lfnowXRatio > m_lfnowYRatio)
                nowRatio = m_lfnowXRatio;
            else
                nowRatio = m_lfnowYRatio;

            // 중심점을 기준점으로 잡음
            mox = (mxMax + mxMin) / 2;
            moy = (myMax + myMin) / 2;
            sox = (sxMax + sxMin) / 2;
            soy = (syMax + syMin) / 2;

            mx = mox + (sx - sox) * nowRatio;
            my = moy - (sy - soy) * nowRatio;
        }

        public void WorldToDevice(double mx, double my, out double sx, out double sy)
        {
            double m_lfnowXRatio = (mxMax - mxMin) / (sxMax - sxMin);  //x축척
            double m_lfnowYRatio = (myMax - myMin) / (syMax - syMin);  //y축척

            // 종횡비 보정
            if (m_lfnowXRatio > m_lfnowYRatio)
                nowRatio = m_lfnowXRatio;
            else
                nowRatio = m_lfnowYRatio;

            // 중심점을 기준점으로 잡음
            mox = (mxMax + mxMin) / 2;
            moy = (myMax + myMin) / 2;
            sox = (sxMax + sxMin) / 2;
            soy = (syMax + syMin) / 2;

            sx = (mx - mox) / nowRatio + sox;
            sy = (moy - my) / nowRatio + soy;
        }

    }
}
